package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.KnowledgeAnnounceListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.KnowledgeAnnounceList;
import com.isunland.managesystem.entity.KnowledgeAnnounceList.KnowledgeAnnounceDetail;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * 知识 公告列表
 *
 * @author wu
 */
public class KnowledgeAnnounceListFragment extends BaseListFragment {

  private static final int REQUEST_QUERY = 1;
  private static final int REQUEST_DETAIL = 2;
  private String mStartDate;
  private String mEndDate;
  private String mTypeName;
  private String mTypeCode;
  private String mKeyWords;
  private KnowledgeAnnounceListAdapter mAdapter;

  @Override public void onCreate(Bundle savedInstanceState) {
    // 设置返回图标
    setHasOptionsMenu(true);
    BaseVolleyActivity activity = (BaseVolleyActivity) getActivity();
    if (activity != null) {
      ActionBar actionBar = activity.getSupportActionBar();
      if (actionBar != null) {
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          actionBar.setDisplayHomeAsUpEnabled(true);
        }
        actionBar.setTitle(R.string.knowledgeAnnounce);
      }
    }
    mStartDate = MyDateUtil.getBeforeThreeMonth();
    //noinspection deprecation
    mEndDate = MyDateUtil.dateToString(new Date(), MyDateUtil.TYPE_DATE);
    mTypeName = "";
    mTypeCode = "";
    mKeyWords = "";
    super.onCreate(savedInstanceState);
  }

  @Override public String getUrl() {

    return ApiConst.URL_ANNOUNCE_LIST;
  }

  @Override public HashMap<String, String> getparameters() {

    HashMap<String, String> params = new HashMap<>();
    params.put("curPage", "1");
    params.put("pageSize", "1000");
    params.put("rangeUserCode", CurrentUser.newInstance(getActivity()).getJobNumber());
    params.put("dataStatus", "checkPass");
    params.put("beginregDate", mStartDate);
    params.put("endregDate", mEndDate);
    params.put("docKindCode", mTypeCode);
    params.put("docKeyWords", mKeyWords);
    params.put("ifGetReadReceipt", "T");
    return params;
  }

  @Override public void resolveResponse(String json) {
    KnowledgeAnnounceList list = new Gson().fromJson(json, KnowledgeAnnounceList.class);
    ArrayList<KnowledgeAnnounceDetail> detail = (ArrayList<KnowledgeAnnounceDetail>) list.getRows();
    setAdapter(detail);
  }

  private void setAdapter(ArrayList<KnowledgeAnnounceDetail> detail) {

    if (detail == null) {
      detail = new ArrayList<>();
    }
    mAdapter = new KnowledgeAnnounceListAdapter(mActivity, detail);
    setListAdapter(mAdapter);
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    KnowledgeAnnounceDetail detail = mAdapter.getItem(position - 1);
    Intent intent = new Intent(getActivity(), KnowledgeAnnounceDetailActivity.class);
    intent.putExtra(KnowledgeAnnounceDetailFragment.EXTRA_VALUE, detail);
    startActivityForResult(intent, REQUEST_DETAIL);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      case R.id.menu_item_query:
        Intent intent = new Intent(getActivity(), KnowLedgeAnnounceSearchActivity.class);
        intent.putExtra(KnowLedgeAnnounceSearchFragment.EXTRA_START_DATE, mStartDate);
        intent.putExtra(KnowLedgeAnnounceSearchFragment.EXTRA_END_DATE, mEndDate);
        intent.putExtra(KnowLedgeAnnounceSearchFragment.EXTRA_TYPE_NAME, mTypeName);
        intent.putExtra(KnowLedgeAnnounceSearchFragment.EXTRA_TYPE_CODE, mTypeCode);
        intent.putExtra(KnowLedgeAnnounceSearchFragment.EXTRA_KEY_WORDS, mKeyWords);
        startActivityForResult(intent, REQUEST_QUERY);
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }

    if (requestCode == REQUEST_QUERY && data != null) {
      mStartDate = data.getStringExtra(KnowLedgeAnnounceSearchFragment.EXTRA_START_DATE);
      mEndDate = data.getStringExtra(KnowLedgeAnnounceSearchFragment.EXTRA_END_DATE);
      mTypeName = data.getStringExtra(KnowLedgeAnnounceSearchFragment.EXTRA_TYPE_NAME);
      mTypeCode = data.getStringExtra(KnowLedgeAnnounceSearchFragment.EXTRA_TYPE_CODE);
      mKeyWords = data.getStringExtra(KnowLedgeAnnounceSearchFragment.EXTRA_KEY_WORDS);
    }
    volleyPost();
    super.onActivityResult(requestCode, resultCode, data);
  }
}
